package com.hymjweb.demo.framework.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class DateUtil {

    /**
     * 取系统当前时间，包含十、分、秒
     *
     * @return
     */
    public static Date getSysDateTime() {
        return new Date();
    }

    public static Date getDateStrToDate(String dateStr, String fmt) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fmt);
        try {
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 取系统当前时间，包含十、分、秒，格式：yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getSysDateTimeStr() {
        return LocalDateTime.now().format(ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 取系统当前日期，不包含十、分、秒，格式：yyyy-MM-dd
     *
     * @return
     */
    public static Date getSysDate() {
        Date d1 = getSysDateTime();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String sDate = sf.format(d1);
        return DateUtil.strToDate(sDate);
    }

    /**
     * 获取系统当前日期,格式：yyyyMMdd
     *
     * @return
     */
    public static String getSysDateStrShort() {
        return LocalDate.now().format(ofPattern("yyyyMMdd"));
    }

    /**
     * 获取系统当前月份,格式：yyyyMM
     *
     * @return
     */
    public static int getSysMonth_Int() {
        return Integer.valueOf(getSysMonth_Str());
    }

    /**
     * 获取系统下一个月份,格式：yyyyMM
     *
     * @return int
     */
    public static int getNextMonthInt() {
        return Integer.valueOf(getNextYYYYMM());
    }

    /**
     * 获取系统当前月份,格式：yyyyMM
     *
     * @return
     */
    public static String getSysMonth_Str() {
        return YearMonth.now().format(ofPattern("yyyyMM"));
    }

    /**
     * 获取系统当前日期,格式：yyyy-MM-dd
     *
     * @return
     */
    public static String getSysDateStr() {
        return LocalDate.now().toString();
    }

    public static String getSysDateStr(String format) {
        return LocalDateTime.now().format(ofPattern(format));
    }


    /**
     * 取得本月第一天的日期
     *
     * @param DQRQ 格式 YYYY-MM-DD
     * @return
     */
    public static String getCurMonthFirstDay(String DQRQ) {
        DQRQ = changeStrDate(DQRQ, "yyyy-MM-dd");
        return YearMonth.parse(DQRQ, ofPattern("yyyy-MM-dd")).atDay(1).toString();
    }

    /**
     * 取得本月最后一天的日期
     *
     * @param DQRQ 格式:YYYYMMDD 或 YYYY-MM-DD 或 YYYY/MM/DD
     * @return
     */
    public static String getCurMonthLastDay(String DQRQ) {
        Date date = strToDate(DQRQ);
        LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.systemDefault());
        return YearMonth.of(ldt.getYear(), ldt.getMonth()).atEndOfMonth().toString();
    }

    /**
     * yql add .... 取得一个月的最大天数
     *
     * @param year
     * @param month
     * @return
     */
    public static int getDaysOfMonth(int year, int month) {
        return Year.of(year).atMonth(month).lengthOfMonth();
    }

    public static String getPreYYYYMM() {
        return YearMonth.now().minusMonths(1).format(ofPattern("yyyyMM"));
    }

    public static String getPreYYYYMM(String currentYYYYMM) {
        return YearMonth.parse(currentYYYYMM, ofPattern("yyyyMM"))
                .minusMonths(1).format(ofPattern("yyyyMM"));
    }

    public static String getNextYYYYMM() {
        return YearMonth.now().plusMonths(1).format(ofPattern("yyyyMM"));
    }

    public static String getNextYYYYMM(String currentYYYYMM) {
        return YearMonth.parse(currentYYYYMM, ofPattern("yyyyMM"))
                .plusMonths(1).format(ofPattern("yyyyMM"));
    }

    /**
     * 取上月第一天
     *
     * @param dqrq Description of Parameter
     * @return String
     * @since 2003-0416
     */
    public static String getFirstDayOfPreMonth(String dqrq) {
        Date date = strToDate(dqrq);
        LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.systemDefault());
        return YearMonth.of(ldt.getYear(), ldt.getMonth()).minusMonths(1).atDay(1).toString();
    }

    /**
     * 获取某一天是星期几,比如2015年5月10号是星期日，则返回7
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static int getWeekByDay(int year, int month, int day) {
        return LocalDate.of(year, month, day).getDayOfWeek().getValue();
    }

    /**
     * Gets the LongStringDate attribute of the DateUtil class
     *
     * @return The LongStringDate value
     */
    public static String getLongStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms");
        return formatter.format(currentTime);
    }

    // add by hejiang 20030804

    /**
     * 取得下个月第一天的日期
     *
     * @param dqrq
     * @return
     */
    public static String getNextMonthFirstDay(String dqrq) {
        Date date = strToDate(dqrq);
        LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.systemDefault());
        return YearMonth.of(ldt.getYear(), ldt.getMonth()).plusMonths(1).atDay(1).toString();
    }


    public static Date getNextSomeDay(Date date, int next) {
        LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.systemDefault()).plusDays(next);
        return Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * Gets the nextSomeDay attribute of the DateUtil class
     *
     * @param strDate Description of the Parameter
     * @param next    Description of the Parameter
     * @return The nextSomeDay value
     */
    public static String getNextSomeMonth(String strDate, int next) {
        try {
            Date date = strToDate(strDate);
            LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.systemDefault());
            return ldt.plusMonths(next).format(ofPattern("yyyy-MM-dd"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Gets the Now attribute of the DateUtil class
     *
     * @return The Now value
     */
    public static Date getNow() {
        return new Date();
    }

    /**
     * 上月第一天
     *
     * @param dqrq
     * @return
     */
    public static String getPreMonthFirstDay(String dqrq) {
        return getFirstDayOfPreMonth(dqrq);
    }

    /**
     * Gets the RightDate attribute of the DateUtil class
     *
     * @param date Description of Parameter
     * @return The RightDate value
     */
    public static int getRightDate(Date date) {
        Calendar rightMonth = Calendar.getInstance();
        if (date != null) {
            rightMonth.setTime(date);
        }
        return rightMonth.get(Calendar.DATE);
    }

    /**
     * Gets the S attribute of the DateUtil class
     *
     * @param strDate Description of Parameter
     * @return The S value
     */
    public static long getS(String strDate) {
        Date date = strToDate(strDate);
        return date.getTime();
    }


    /**
     * Gets the StringDate attribute of the DateUtil class
     *
     * @param format Description of Parameter
     * @return The StringDate value
     */
    public static String getStringDate(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(new Date());
    }

    /**
     * Gets the StringDateShort attribute of the DateUtil class
     *
     * @return The StringDateShort value
     */
    public static String getStringDateShort() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(new Date());
    }

    /**
     * 取得指定的日期加最小时间，即去掉时分秒
     *
     * @return
     */
    public static Date getDayMin(Date d) {
        Date thisd = new Date(d.getTime());
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String ds = f.format(thisd);
        try {
            return f.parse(ds);
        } catch (Exception e) {
            //NutzUtil.getLogger().error(e);
            return null;
        }
    }

    /**
     * 取得指定的日期加最大时间，即加上 23:59:59
     *
     * @return
     */
    public static Date getDayMax(Date d) {
        Date thisd = new Date(d.getTime());
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String ds = f.format(thisd) + " 23:59:59";
        try {
            f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return f.parse(ds);
        } catch (Exception e) {
            //NutzUtil.getLogger().error(e);
            return null;
        }
    }

    /**
     * add by yql 校验日期是否合法！
     *
     * @param strDate 日期字符串，例如2003-03-05或者20030507;
     * @return 合法，返回true;非法，返回false
     */
    public static boolean isValidDate(String strDate) {
        if (strDate == null) {
            return false;
        }

        // 2021-04-25T16:00:00.000+0000
        strDate = strDate.trim();
        int len = strDate.length();
        if (len != 10 && len != 8 && len != 19 && len != 18 && len != 28) {
            return false;
        }

        int iYear = 0;
        int iMonth = 0;
        int iDay = 0;
        try {
            if (strDate.length() > 10) {
                strDate = strDate.substring(0, 10);
            }
            if (strDate.length() == 10) {
                iYear = Integer.parseInt(strDate.substring(0, 4));
                iMonth = Integer.parseInt(strDate.substring(5, 7));
                iDay = Integer.parseInt(strDate.substring(8, 10));
            } else {
                iYear = Integer.parseInt(strDate.substring(0, 4));
                iMonth = Integer.parseInt(strDate.substring(4, 6));
                iDay = Integer.parseInt(strDate.substring(6, 8));
            }
            LocalDate.of(iYear, iMonth, iDay);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 检查是否是合法的年月
     *
     * @param yearMonth 例如"201201"
     * @return
     */
    public static boolean isValidYearMonth(String yearMonth) {
        String date = yearMonth + "01";
        return isValidDate(date);
    }

    public static boolean isValidYearMonth(long yearMonth) {
        return isValidYearMonth(yearMonth + "");
    }

    /**
     * Description of the Method
     *
     * @param dateDate Description of Parameter
     * @return Description of the Returned Value
     */
    public static String dateToStr_yyyyMMdd(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        return formatter.format(dateDate);
    }

    /**
     * yyyy-MM-dd HH:mm:ss格式
     *
     * @param dateDate
     * @return Description of the Returned Value
     */
    public static String dateToStr_yyyy_MM_dd_HH_mm_ss(Date dateDate) {
        if (dateDate == null)
            return "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(dateDate);
    }

    /**
     * yyyy-MM-dd 格式
     *
     * @param dateDate
     * @return
     */
    public static String dateToStr_yyyy_MM_dd(Date dateDate) {
        if (dateDate == null){
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(dateDate);
    }

    /**
     * 转换日期为字符串格式
     * <p>
     * <p>
     * add by wjz 1125
     *
     * @param dateDate
     * @param format
     * @return
     */
    public static String dateToStr(Date dateDate, String format) {
        if (dateDate == null)
            return "";
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            return formatter.format(dateDate);
        } catch (Exception ex) {
            //NutzUtil.getLogger().error(ex.getMessage(), ex);
            return "";
        }

    }


    /**
     * 计算两个日期之间相隔的天数
     *
     * @param date1 Description of Parameter
     * @param date2 Description of Parameter
     * @return 返回相隔的天数
     */
    public static int diffDate(Date date1, Date date2) {
        return (int) ((date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000));
    }


    public static int diffDate(Long date1TimeMillis,Long data2TimeMillis){
        Date date1 = new Date(date1TimeMillis);
        Date date2 = new Date(data2TimeMillis);
        return  diffDate(date1,date2);
    }


    /**
     * 计算两个日期之间相隔的秒数
     *
     * @param date1 Description of Parameter
     * @param date2 Description of Parameter
     * @return 返回相隔的秒数
     */
    public static int diffSecond(Date date1, Date date2) {
        return (int) ((date1.getTime() - date2.getTime()) / (1000));
    }

    /**
     * 计算两个日期之间相隔的分钟数
     *
     * @param date1 Description of Parameter
     * @param date2 Description of Parameter
     * @return 返回相隔的分钟数
     */
    public static int diffMinute(Date date1, Date date2) {
        return diffSecond(date1, date2) / 60;
    }


    public static String changeStrDate(String strDate, String strFormat) {
        Date d = strToDate(strDate);
        return dateToStr(d, strFormat);
    }

    public static String changeStrDate(Date d, String strFormat) {
        return dateToStr(d, strFormat);
    }

    /**
     * 转化为：yyyy-MM-dd
     *
     * @param d
     * @return
     */
    public static String changeStrDate_yyyy_MM_dd(Date d) {
        return dateToStr(d, "yyyy-MM-dd");
    }

    /**
     * 将字符串格式的日期转换为Date型的日期
     * <p>
     * <p>
     * modify by wjz 0326
     * <p>
     * <p>
     * 考虑到日期格式比较复杂，在转换之前先做如下假定：
     * <p>
     * <p>
     * 都是按照年－月－日的格式排列
     * <p>
     * <p>
     * 年都是4位
     * <p>
     *
     * @param strDate Description of Parameter
     * @return Description of the Returned Value
     */



    public static Date strToDate(String strDate) {
        if (strDate == null || strDate.length() < 6) {
            throw new IllegalArgumentException("illegal date format");
        }
        String fmt = "yyyy-MM-dd HH:mm:ss";
        int len = strDate.length();
        if(len>19){ // 2021-03-03T16:00:00.000+0000
            strDate = strDate.replaceAll("T", " ");
            strDate = strDate.substring(0,19);
        } else if (len == 19) { // 2021-03-03T16:00:00
            strDate = strDate.replaceAll("T", " ");
            if (strDate.indexOf("-") > 0) {
                fmt = "yyyy-MM-dd HH:mm:ss";
            } else if (strDate.indexOf("/") > 0) {
                fmt = "yyyy/MM/dd HH:mm:ss";
            }
        } else if (len == 18) {// 2021-03-0316:00:00
            if (strDate.indexOf("-") > 0) {
                fmt = "yyyy-MM-ddHH:mm:ss";
            } else if (strDate.indexOf("/") > 0) {
                fmt = "yyyy/MM/ddHH:mm:ss";
            }
        } else if (len == 14) {// 20210303160000
            fmt = "yyyyMMddHHmmss";
        } else if (len == 10) { //2021-03-03
            if (strDate.indexOf("-") > 0) {
                fmt = "yyyy-MM-dd";
            } else if (strDate.indexOf("/") > 0) {
                fmt = "yyyy/MM/dd";
            } else if (strDate.indexOf(".") > 0) {
                fmt = "yyyy.MM.dd";
            }
        } else if (len == 8) {//21-03-0316:00:00
            if (strDate.indexOf("-") > 0) {
                fmt = "yy-MM-dd";
            } else if (strDate.indexOf("/") > 0) {
                fmt = "yy/MM/dd";
            } else if (strDate.indexOf(".") > 0) {
                fmt = "yy.MM.dd";
            } else {
                fmt = "yyyyMMdd";
            }
        } else if (len == 7) {// 2021-03
            if (strDate.indexOf("-") > 0) {
                fmt = "yyyy-MM";
            } else if (strDate.indexOf("/") > 0) {
                fmt = "yyyy/MM";
            } else if (strDate.indexOf(".") > 0) {
                fmt = "yyyy.MM";
            }
        } else if (len == 6) {//202103
            fmt = "yyyyMM";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(fmt);
        ParsePosition pos = new ParsePosition(0);
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+24:00"));
        return formatter.parse(strDate, pos);
    }

    public static String getWeekDayCN(Date d) {
        return getWeekDayCN(dateToStr_yyyy_MM_dd_HH_mm_ss(d));
    }

    public static String getWeekDayCN(String sdate) {
        Date date = strToDate(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int week = c.get(Calendar.DAY_OF_WEEK);
        HashMap<Integer, String> m = new HashMap<Integer, String>();
        m.put(0, "日");
        m.put(1, "一");
        m.put(2, "二");
        m.put(3, "三");
        m.put(4, "四");
        m.put(5, "五");
        m.put(6, "六");
        return m.get((week - 1));
    }

    /***
     * 返回月份，格式yyyyMM
     *
     * @param d
     * @return yyyyMM
     */
    public static int date2YearMonth(Date d) {
        if (d == null)
            return -1;
        return DataTypeUtil.toInt(dateToStr(d, "yyyyMM"), -1);
    }

    public static int date2short(Date d) {
        if (d == null)
            return -1;
        return DataTypeUtil.toInt(dateToStr(d, "yyyyMMdd"), -1);
    }

    public static int date2short(String d) {
        if (d == null)
            return -1;
        return DataTypeUtil.toInt(changeStrDate(d, "yyyyMMdd"), -1);
    }

    /**
     * 获取日历形式的一个月的二维数组形式,如果为空表示没有,例如这样的<BR>
     * 2015年 1月<BR>
     * <BR>
     * ===========================<BR>
     * 一 二 三 四 五 六 日<BR>
     * 1 2 3 (二维数组第1行)<BR>
     * 4 5 6 7 8 9 10 (二维数组第2行)<BR>
     * 11 12 13 14 15 16 17 (二维数组第3行)<BR>
     * 18 19 20 21 22 23 24 (二维数组第4行)<BR>
     * 25 26 27 28 29 30 31 (二维数组第5行)<BR>
     *
     * @param year
     * @param month
     * @return
     */
    public static Object[][] get2ArrDaysOfMonth(int year, int month) {
        List<Object[]> list = new ArrayList<Object[]>();
        int week = getWeekByDay(year, month, 1);
        Object[] row = new Object[7];// 存放一个星期的数据
        for (int j = 1; j <= week - 1; j++) {// 按每个月第一天是星期几，之前的置空
            row[j - 1] = "";
        }
        int day = 1;
        for (int j = week; j <= 7; j++) {
            row[j - 1] = day;
            day++;
        }
        list.add(row);
        day--;

        int monthDays = getDaysOfMonth(year, month); // 获取每个月的天数
        for (int d = day; d < monthDays; d++) {// 每过一天后，反应第二天是星期几
            week = getWeekByDay(year, month, d);
            if (week == 1) {// 如果第二天是星期一，便换行。
                row = new Object[7];
                for (int j = 1; j <= 7; j++) {
                    day++;
                    if (day > monthDays)
                        break;
                    row[j - 1] = day;
                }
                list.add(row);
            }
        }
        return list.toArray(new Object[0][0]);
    }


    public static String getSysIdLocal() {
        return SnowflakeSequence.getId();
    }

    /**
     * 分段获取指定日期期间的日期起止，例如2015-05-01至2015-05-31，限制天数为10时，
     * <br>返回的数组是这样的[[2015-05-01,2015-05-10],[2015-05-11,2015-05-20],[2015-05-21,2015-05-30],[2015-05-31,2015-05-31]]
     *
     * @param startDate 起始日期
     * @param endDate   结束日期
     * @param limitDays 限制天数
     * @return
     */
    public static Date[][] getDate2ArrByLimit(Date startDate, Date endDate, int limitDays) {
        Date thisStartDate = getDayMin(startDate);
        Date thisEndDate = getDayMin(endDate);
        int diffDays = DateUtil.diffDate(thisEndDate, thisStartDate) + 1;
        // final int LIMIT_DAYS = 10;
        int mod = diffDays / limitDays;
        int yu = diffDays % limitDays;
        int loopCount = (yu == 0 ? mod : mod + 1);

        Date[][] arr = new Date[loopCount][2];
        for (int i = 0; i < loopCount; i++) {
            Date st2 = (i == 0 ? thisStartDate : DateUtil.getNextSomeDay(thisStartDate, i * limitDays));
            Date et2 = (i == loopCount - 1) ? thisEndDate
                    : DateUtil.getNextSomeDay(thisStartDate, i * limitDays + limitDays - 1);
            arr[i][0] = st2;
            arr[i][1] = et2;
        }
        return arr;
    }

    public static String getUUID() {
        String uuid = "";
        for (int i = 0; i < 10; i++) {
            uuid = UUID.randomUUID().toString().replaceAll("-", "");
        }
        return uuid;
    }

    /**
     * 验证日期字符串是否为合法YYYY-MM-DD格式 (例如2018-03-23为有效，2018-33-33为无效)
     *
     * @param str 日期格式，YYYY-MM-DD
     * @return
     * @author 张凯
     * @serialDate 2018年3月23日
     */
    public static boolean isDataFormat(String str) {
        boolean flag = false;
        String regxStr = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
        Pattern pattern1 = Pattern.compile(regxStr);
        Matcher isNo = pattern1.matcher(str);
        if (isNo.matches()) {
            flag = true;
        }
        return flag;
    }

    /**
     * 根据现有年月+偏移月份获取到新的年月
     *
     * @param yearMonth YYYYMM型的int数据
     * @param offset    偏移月份,支持正向反向偏移
     * @return
     * @author 余伟
     * @serialData 2018年7月5日
     */
    public static Long getNextSomeMonth(int yearMonth, int offset) {
        String sdate = yearMonth + "01";
        sdate = getNextSomeMonth(sdate, offset);
        sdate = changeStrDate(sdate, "yyyyMM");
        return Long.parseLong(sdate);
    }

    /**
     * 根据现有年月+偏移月份获取到新的年月
     *
     * @param yearMonth YYYYMM型的long数据
     * @param offset    偏移月份,支持正向反向偏移
     * @return
     * @author admin
     * @serialData 2018年7月5日
     */
    public static Long getNextSomeMonth(long yearMonth, int offset) {
        return getNextSomeMonth((int) yearMonth, offset);
    }

    /**
     * 传入"2018-6-24" 格式的日期字符串，返回一个数组，2018-6-24 00:00:00 对应的long值及2018-6-24 23:59:59对应的long值
     *
     * @param date
     * @return
     * @author 罗贤虎
     */
    public static String[] getDateStartEnd(String date) {
        String[] ret = new String[2];
        try {
            Date dayDate = strToDate(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dayDate);
            ret[0] = calendar.getTimeInMillis() + "";

            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            ret[1] = calendar.getTimeInMillis() + "";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 获取当前月份除周六日外的天数
     *
     * @return
     * @author 余伟
     * @serialData 2018年7月15日
     */
    public static int getThisMonthOfWorkDays() {
        return getWorkDaysOfThisMonth(true);
    }

    /**
     * 到现在为止的工作日数
     *
     * @return
     * @author 余伟
     * @serialData 2018年7月15日
     */
    public static int getWorkDaysOfThisMonth(boolean isCurrentDay) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        cal.set(year, month, 1);
        int count = 0;
        while (cal.get(Calendar.MONTH) == month) {
            int newDay = cal.get(Calendar.DAY_OF_WEEK);
            if (!(newDay == Calendar.SUNDAY || newDay == Calendar.SATURDAY)) {
                if (!isCurrentDay && day > cal.get(Calendar.DAY_OF_MONTH)) {
                    count++;
                } else if (isCurrentDay) {
                    count++;
                }
            }
            cal.add(Calendar.DATE, 1);
        }
        return count;
    }

    /**
     * 根据字符串模式获取日期时间模版对象
     *
     * @param format 字符串模式
     * @return DateTimeFormatter
     * @author 章勇
     * @date 2019/05/31
     */
    private static DateTimeFormatter ofPattern(String format) {
        return DateTimeFormatter.ofPattern(format);
    }

    public static long getCurrentTimeLong() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1; // 默认是0--11,所以要加1
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        return Long.parseLong("" + year + (month < 10 ? "0" : month) + (day < 10 ? "0" + day : day) + hour + (minute < 10 ? "0" + minute : minute) + (second < 10 ? "0" + second : second));
    }


    /**
     * 根据当前时间，添加或减去指定的时间量。例如，要从当前日历时间减去 5 天，可以通过调用以下方法做到这一点：
     * add(Calendar.DAY_OF_MONTH, -5)。
     * @param date 指定时间
     * @param num  为时间添加或减去的时间天数
     * @return
     */
    public static Date getBeforeOrAfterDate(Date date, int num) {
        Calendar calendar = Calendar.getInstance();//获取日历
        calendar.setTime(date);//当date的值是当前时间，则可以不用写这段代码。
        calendar.add(Calendar.DATE, num);
        Date d = calendar.getTime();//把日历转换为Date
        return d;
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String dateToStr(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }


}
